package com.chuzhiyun.chengguoxy.feature.internship.worktags;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.ActivityWorkTasksInfoBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseActivity;
import com.chuzhiyun.chengguoxy.feature.bean.TasksInfoBean;
import com.chuzhiyun.chengguoxy.feature.bean.WorkTagsConfigBean;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.LocationUtils;
import com.chuzhiyun.chengguoxy.utlis.StringUtils;
import com.chuzhiyun.chengguoxy.utlis.TimeUtils;
import com.codingending.popuplayout.PopupLayout;
import com.google.gson.Gson;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@SuppressWarnings("ALL")
public class WorkTasksInfoActivity extends BaseActivity<ActivityWorkTasksInfoBinding> {
    private String tasksId;
    private WorkTagsConfigBean configBean;
    private String id;

    private String titleValue;//标题
    private String dateValue;//日期
    private String textValue;//文本内容
    private String latitude;
    private String longitude;
    private String adminDivision;
    private String street;
    private ArrayList<File> addFiles = new ArrayList<>();


    @Override
    public void initView(View view) {
        String tasksName = getIntent().getStringExtra("tasksName");
        tasksId = getIntent().getStringExtra("tasksId");
        id = getIntent().getStringExtra("id");
        viewBinding.navView.tvTitle.setText(tasksName);
        viewBinding.navView.ivBack.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onBackPressed();
            }
        });
        getWorkTags();
        getWorkTasks();
        initEditLength(viewBinding.editLiyou,viewBinding.tvLengthTip,500);

        viewBinding.titleEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                titleValue = viewBinding.titleEdit.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        viewBinding.messageTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                textValue = viewBinding.messageTitle.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        viewBinding.xuanzeRl.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                showBottomTimeChoose();
            }
        });
        viewBinding.refreshText.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                getLocalInfo();
            }
        });

        viewBinding.localBtn.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                getLocalInfo();
            }
        });

        viewBinding.tvSubmit.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                submitClick();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {

    }

    private void getLocalInfo(){
        try {
            LocationUtils.getInstance().getLocation(mContext, new LocationUtils.OnLocationChangedListener() {
                @Override
                public void onSuccess(AMapLocation amapLocation) {
                    street = amapLocation.getAoiName();
                    adminDivision = amapLocation.getAdCode();
                    longitude = String.format("%.6f",amapLocation.getLongitude());
                    latitude = String.format("%.6f",amapLocation.getLatitude());
                    viewBinding.addressText.setText(street);
                    if (!StringUtils.isBlank(street)){
                        viewBinding.localbtnLl.setVisibility(View.GONE);
                        viewBinding.addressInfoRl.setVisibility(View.VISIBLE);
                    }
                    else {
                        viewBinding.localbtnLl.setVisibility(View.VISIBLE);
                        viewBinding.addressInfoRl.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onFail(int errCode, String errInfo) {
                    System.out.println(errCode);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getWorkTags(){
        String url = NetApi.getWorkTagsConfig(tasksId);
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    configBean = gson.fromJson(response, WorkTagsConfigBean.class);
                    setInputView();
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    private void setInputView(){
        if (configBean.isEnableTitle()){
            viewBinding.titleRl.setVisibility(View.VISIBLE);
            viewBinding.line0.setVisibility(View.VISIBLE);
            if (configBean.isTitleRequired()){
                viewBinding.titleRequiredRl.setVisibility(View.VISIBLE);
                viewBinding.titleTv2.setVisibility(View.GONE);
            }
            else {
                viewBinding.titleRequiredRl.setVisibility(View.GONE);
                viewBinding.titleTv2.setVisibility(View.VISIBLE);
            }
        }
        else {
            viewBinding.titleRl.setVisibility(View.GONE);
            viewBinding.line0.setVisibility(View.GONE);
        }

        if (configBean.isEnableDate()){
            viewBinding.dateRl.setVisibility(View.VISIBLE);
            viewBinding.line1.setVisibility(View.VISIBLE);
            if (configBean.isDateRequired()){
                viewBinding.dateRequiredRl.setVisibility(View.VISIBLE);
                viewBinding.titleTv3.setVisibility(View.GONE);
            }
            else {
                viewBinding.dateRequiredRl.setVisibility(View.GONE);
                viewBinding.titleTv3.setVisibility(View.VISIBLE);
            }
        }
        else {
            viewBinding.dateRl.setVisibility(View.GONE);
            viewBinding.line1.setVisibility(View.GONE);
        }

        if (configBean.isEnableText()){
            viewBinding.messageLl.setVisibility(View.VISIBLE);
            viewBinding.line3.setVisibility(View.VISIBLE);
            if (configBean.isTextRequired()){
                viewBinding.textRequiredRl.setVisibility(View.VISIBLE);
                viewBinding.messageTitle.setVisibility(View.GONE);
            }
            else {
                viewBinding.textRequiredRl.setVisibility(View.GONE);
                viewBinding.messageTitle.setVisibility(View.VISIBLE);
            }
        }
        else {
            viewBinding.messageLl.setVisibility(View.GONE);
            viewBinding.line3.setVisibility(View.GONE);
        }

        if (configBean.isEnableFile()){
            viewBinding.pictureLl.setVisibility(View.VISIBLE);
            if (configBean.isFileRequired()){
                viewBinding.pictureRequiredRl.setVisibility(View.VISIBLE);
                viewBinding.pictureTitleTv.setVisibility(View.GONE);
            }
            else {
                viewBinding.pictureRequiredRl.setVisibility(View.GONE);
                viewBinding.pictureTitleTv.setVisibility(View.VISIBLE);
            }
        }
        else {
            viewBinding.pictureLl.setVisibility(View.GONE);
        }

        if (configBean.isEnablePosition()){
            viewBinding.addressLl.setVisibility(View.VISIBLE);
            if (configBean.isPositionRequired()){
                viewBinding.addressRequiredRl.setVisibility(View.VISIBLE);
                viewBinding.addressTitleTv.setVisibility(View.GONE);
            }else {
                viewBinding.addressRequiredRl.setVisibility(View.GONE);
                viewBinding.addressTitleTv.setVisibility(View.VISIBLE);
            }
        }else {
            viewBinding.addressLl.setVisibility(View.GONE);
        }
    }

    private void getWorkTasks(){
        String url = NetApi.getTaskInfo(id);
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    TasksInfoBean bean = gson.fromJson(response,TasksInfoBean.class);
                    titleValue = bean.getTitleValue();
                    dateValue = bean.getDateValue();
                    textValue = bean.getTextValue();
                    if (bean.getPosition()!=null){
                        longitude = bean.getPosition().getLongitude();
                        latitude = bean.getPosition().getLatitude();
                        adminDivision = bean.getPosition().getAdminDivision()+"";
                        street = bean.getPosition().getStreet();
                    }
                    if (!StringUtils.isBlank(titleValue)){
                        viewBinding.titleEdit.setText(titleValue);
                    }
                    if (!StringUtils.isBlank(dateValue)){
                        viewBinding.chooseTime.setText(dateValue);
                    }
                    if (!StringUtils.isBlank(street)){
                        viewBinding.addressText.setText(street);
                        viewBinding.addressInfoRl.setVisibility(View.VISIBLE);
                        viewBinding.localbtnLl.setVisibility(View.GONE);
                    }
                    else {
                        viewBinding.addressInfoRl.setVisibility(View.GONE);
                        viewBinding.localbtnLl.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    private void submitClick(){

    }

    private void showBottomTimeChoose(){

        //时间选择器
        View view=View.inflate(mContext, R.layout.timechoose_menu,null);
        PopupLayout popupLayout = PopupLayout.init(mContext,view);
        popupLayout.show();

        dateValue = TimeUtils.getCurrentTime();

        TextView monthText = view.findViewById(R.id.month_Text);
        CalendarView calendarView = view.findViewById(R.id.calendarView);
        ImageView calendarLeft = view.findViewById(R.id.left_Text);
        ImageView calendarRight = view.findViewById(R.id.right_Text);
        calendarLeft.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                calendarView.scrollToPre();
            }

        });
        calendarRight.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                calendarView.scrollToNext();
            }

        });

        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                String dateStr = null;
                try {
                    dateStr = TimeUtils.strToDateFormat(calendar.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                dateValue = dateStr;
            }
        });

        java.util.Calendar calendar = java.util.Calendar.getInstance();
        int y = calendar.get(java.util.Calendar.YEAR);
        int m = calendar.get(java.util.Calendar.MONTH);

        @SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
        calendar.setTime(new Date());
        String monthString = df.format(calendar.getTime());
        monthText.setText(monthString);
        calendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @SuppressLint("DefaultLocale")
            @Override
            public void onMonthChange(int year, int month) {
                if (month<10)
                {
                    monthText.setText(String.format("%d年0%d月", year, month));
                }
                else
                {
                    monthText.setText(String.format("%d年%d月", year, month));
                }
            }
        });

        TextView cancelText = view.findViewById(R.id.cancel_Text);
        TextView sureText = view.findViewById(R.id.sure_Text);
        cancelText.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                popupLayout.dismiss();
            }

        });
        sureText.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                viewBinding.chooseTime.setText(dateValue);
                popupLayout.dismiss();
            }
        });
    }


    @Override
    protected ActivityWorkTasksInfoBinding getViewBinding() {
        return ActivityWorkTasksInfoBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }
}